from tornado.web import RequestHandler
# 支持websocker
from tornado.websocket import WebSocketHandler
from app.charts.charts import ProvinceMap
from app.dao.datasource import DataSource
import json

class Homehandler(RequestHandler):
    def get(self):
        # self.write('你好 ')
        map = ProvinceMap()
        html = map.get_html()
        self.render('home.html',x={'num':100,'html':html})
users = set()
class UpdateHandler(WebSocketHandler):
    # 接收客服端的请求信息,自动调用
    def open(self, *args: str, **kwargs: str):
        # print('接受到了请求')
        users.add(self)
    # 接受客服端发送的消息
    def on_message(self,message):
        # print('接受到了客服端的内容是',message)
        for user in users:
            ds = DataSource()
            user.write_message(json.dumps(ds.get_values()))

    #
    def on_close(self):
        users.discard(self)
    # def get(self):
    #     ds = DataSource()
    #     print(ds.get_values())
    #     self.write(json.dumps(ds.get_values()))